﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.Web.UserControls;
using NetBpm.Util.Client;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;

namespace BEGDC.CC.Web
{
	/// <summary>
	/// Summary description for main.
	/// </summary>
	public class MyDelegation : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.TextBox txtStartDate;
		protected System.Web.UI.HtmlControls.HtmlImage StartDateImg;
		protected System.Web.UI.WebControls.TextBox txtEndDate;
		protected System.Web.UI.HtmlControls.HtmlImage EndlDateImg;
		protected BEGDC.CC.Web.Control.UserSearchControl SearchStaff;
		protected System.Web.UI.WebControls.CheckBox chkIsEnabled;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				// Put user code to initialize the page here
				if(!Page.IsPostBack)
				{
                    this.Page.Response.Expires = 0;
					this.GetUserInfo();
					
				}
			}
			catch(GDCException gdcEx)
			{
				WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
			}
			catch(Exception ex)
			{
				string s = ex.Message;
				WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
			}
		}

		private void GetUserInfo()
		{
			IUser iuser = AppContainer.Instance[ServiceConst.UserService] as IUser;
//			System.Collections.IList employees = iuser.GetUserList();
//
//			this.ddlDelegation.Items.Add(string.Empty);
//			string myid = ((BEGDC.CC.BL.Workflow.CCIdentity)Page.User.Identity).UserInfo.Id;
//			foreach(BEGDC.CC.Entity.Employee employee in employees)
//			{
//				if(employee.Id != myid);
//					this.ddlDelegation.Items.Add(new System.Web.UI.WebControls.ListItem(employee.NameEN,employee.Id));
//
//			}

			int userroleId = Convert.ToInt32(Page.Request.QueryString["roleid"]);

			BEGDC.CC.Entity.UserRole role = iuser.GetUserRoleByID(userroleId);

			if (Request.QueryString["type"] == null)
			{
				this.SearchStaff.SelfID = ((BEGDC.CC.BL.Workflow.CCIdentity)Page.User.Identity).UserInfo.Id;
			}
			else
			{
				this.SearchStaff.SelfID = role.Employee.Id;
			}


			if (role.DelegateUser != null)
			{
				this.SearchStaff.EmployeeID = role.DelegateUser.Id;
				this.SearchStaff.EmployeeName = role.DelegateUser.NameEN;
				this.chkIsEnabled.Checked = role.IsEnabled;

				//this.ddlDelegation.SelectedValue = role.DelegateUser.Id;
			}
			if (role.DelegateStartDate == Nullables.NullableDateTime.Default)
			{
				this.txtStartDate.Text = System.DateTime.Now.ToString("M/d/yyyy");
				this.txtEndDate.Text = System.DateTime.Now.AddDays(14).ToString("M/d/yyyy");
			}
			else
			{
				this.txtStartDate.Text = role.DelegateStartDate.Value.ToString("M/d/yyyy");
				this.txtEndDate.Text = role.DelegateEndDate.Value.ToString("M/d/yyyy");
			}


		}



		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnSave.Click += new EventHandler(btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


        void btnSave_Click(object sender, EventArgs e)
		{
			IUser iuser = AppContainer.Instance[ServiceConst.UserService] as IUser;

			int userroleId = Convert.ToInt32(Page.Request.QueryString["roleid"]);

			BEGDC.CC.Entity.UserRole role = iuser.GetUserRoleByID(userroleId);
			role.IsEnabled = this.chkIsEnabled.Checked;
			if (this.SearchStaff.EmployeeID == string.Empty)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.US007 + "');</script>");
				return;

				role.DelegateUser = null;
				role.DelegateStartDate = Nullables.NullableDateTime.Default;
				role.DelegateEndDate = Nullables.NullableDateTime.Default;

				// Write Log
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				CCIdentity id = this.User.Identity as CCIdentity;
				BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
				log.Module = "UserManagement";
				log.OperationType = "Delegation Role";
				Employee emp = new Employee();
				emp.Id = id.UserInfo.Id;
				log.Employee = emp;
				log.Time = System.DateTime.Now;
				log.Description = "[Delegation Role:Cancel Delegation";
				utility.SaveOperationLog(log);
			}
			else
			{

				if (this.txtStartDate.Text == string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + string.Format(BEGDC.CC.Resource.SystemMessage.SM019,"Start Date") + "');</script>");
					return;
				}
				if (this.txtEndDate.Text == string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + string.Format(BEGDC.CC.Resource.SystemMessage.SM019,"End Date") + "');</script>");
					return;
				}
					
				BEGDC.CC.Entity.Employee emp = new Employee();
				emp.Id = this.SearchStaff.EmployeeID;
				role.DelegateUser = emp;
			
				role.DelegateStartDate = Nullables.NullableDateTime.Parse(this.txtStartDate.Text);
				role.DelegateEndDate = Nullables.NullableDateTime.Parse(this.txtEndDate.Text);

				if (Convert.ToDateTime(this.txtStartDate.Text) > Convert.ToDateTime(txtEndDate.Text))
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.US005 + "');</script>");
					return;
				}

				// Write Log
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				CCIdentity id = this.User.Identity as CCIdentity;
				BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
				log.Module = "UserManagement";
				log.OperationType = "Delegation Role";
				emp = new Employee();
				emp.Id = id.UserInfo.Id;
				log.Employee = emp;
				log.Time = System.DateTime.Now;
				log.Description = "[Delegation Role:Delegation User:" + role.Employee.NameCN + ". Start Date:" + role.DelegateStartDate.Value.ToString("M/d/yyyy") + ". EndDate:" + role.DelegateEndDate.Value.ToString("M/d/yyyy");
				utility.SaveOperationLog(log);
			}
			if (iuser.SaveUserRole(role))
			{
				//Clear Cache
				BEGDC.CC.BL.Workflow.CCPrincipal.RemovePrincipal(this.SearchStaff.EmployeeName);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();window.returnValue='reload';</script>");
			}
			else
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			
		}

	}
}
